package com.weipai.springboot.module.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.weipai.springboot.enums.RetCode;
import com.weipai.springboot.model.ProductCategory;
import com.weipai.springboot.model.api.Response;
import com.weipai.springboot.module.service.FileUploadService;
import com.weipai.springboot.module.service.ProductCategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author: 曹朋飞
 * @Date: 2018/07/27 17:51
 *
 * 商品类别管理类
 */
@RestController
public class ProductCategoryController {

    @Autowired
    ProductCategoryService productCategoryService;
    @Autowired
    FileUploadService fileUploadService;


    /**
     * 添加分类
     * @param request
     * @param productCategory
     * @param img
     * @return
     */
    @PostMapping("/productCategory/save")
    public Response save(HttpServletRequest request, ProductCategory productCategory, MultipartFile img){
        Response response = new Response();
        Map<String, Object> map = new HashMap<>();
        if(img!=null){
            String result = fileUploadService.saveImgUpload(request, img);
            if (result.equals("formatError")) {
                response.setCode(RetCode.IMG_FORMAT);
            } else if (result.equals("sizeover")) {
                response.setCode(RetCode.IMG_SIZE);
            } else if(result.equals("error")){
                response.setCode(RetCode.IMG_ERROR);
            }else{
                productCategory.setProductCategoryImg(result);
                response = productCategoryService.save(productCategory);
            }
        }else{
            response = productCategoryService.save(productCategory);
        }
        return response;
    }

    /**
     * 修改分类
     * @param request
     * @param productCategory
     * @param img
     * @return
     */
    @PutMapping("/productCategory/update")
    public Response update(HttpServletRequest request, ProductCategory productCategory, MultipartFile img){
        Response response = new Response();
        if(img!=null){
            String result = fileUploadService.saveImgUpload(request, img);
            if (result.equals("formatError")) {
                response.setCode(RetCode.IMG_FORMAT);
            } else if (result.equals("sizeover")) {
                response.setCode(RetCode.IMG_SIZE);
            } else if(result.equals("error")){
                response.setCode(RetCode.IMG_ERROR);
            }else{
                productCategory.setProductCategoryImg(result);
                response = productCategoryService.update(productCategory);
            }
        }else{
            response = productCategoryService.update(productCategory);
        }
        return response;
    }

    /**
     * 查询货品类别详情
     * @param id:货品类别的id
     * @return
     */
    @GetMapping("/productCategory/getProductCategoryById/{id}")
    public Response getProductCategoryById(@PathVariable("id")  String id){
        return productCategoryService.getProductCategoryById(id);
    }

    /**
     * 分页查询数量
     * @return
     */
    @GetMapping("/productCategory/selectAllByRank/{page}/{size}/{rank}/{draw}/{sort}/{way}/{id}")
    public Map<String, Object> selectAllByRank(@PathVariable("page") Integer page,
                                               @PathVariable("size") Integer size,
                                               @PathVariable("rank") String rank,
                                               @PathVariable("draw") String draw,
                                               @PathVariable("sort") String sort,
                                               @PathVariable("way") String way,
                                               @PathVariable("id")  String goodsCategoryId){
        Map<String, Object> map = new HashMap<>();
        PageHelper.startPage(page, size);
        List<ProductCategory> list = productCategoryService.selectAllByRankDel(0,rank, sort, way, goodsCategoryId);
        PageInfo<ProductCategory> pageInfo = new PageInfo<>(list);
        if(list.size()>0){
            map.put("pageData", pageInfo.getList());
            map.put("recordsTotal", pageInfo.getTotal());
            map.put("draw", draw);
            map.put("pages", pageInfo.getPages());
            map.put("status",200);
        }else{
            map.put("status",401);
            map.put("msg","暂无数据");
        }
        return map;
    }

    /**
     * 更改货品类别的状态---禁用还是启用
     * @param id
     * @return
     */
    @GetMapping("/productCategory/changeStatus/{id}/{status}")
    public Response changeStatus(@PathVariable("id")  String id,
                                            @PathVariable("status")  Integer status){
        return productCategoryService.changeStatus(id, status);
    }

    /**
     * 批量删除
     * @param id:货品分类的id集合
     * @return
     */
    @DeleteMapping("/productCategory/batchDeleteById/{id}")
    public Response batchDeleteById(@PathVariable("id")  String[] id){
        return productCategoryService.batchDeleteById(id);
    }

    /**
     * 查询三级分类
     * @return
     */
    @GetMapping("/productCategory/getAllProductCategory")
    public Response getAllProductCategory(){
        return productCategoryService.getAllProductCategoryV2();
    }

    /**
     * 首页工具一级分类接口
     * @return
     */
    @GetMapping("/productCategory/getTopCategory")
    public Response getTopCategory(){
        return productCategoryService.getChildCategory("null");
    }

    /**
     * 工具二级分类接口    APP
     * @param id
     * @return
     */
    @GetMapping("/productCategory/getChildCategory/{id}")
    public Response getChildCategory(@PathVariable("id") String id){
        return productCategoryService.getChildCategory(id);
    }

    /**
     *根据三级分类id查询商品搜索中规格选项条件
     * @param id
     * @return
     */
    @GetMapping("/productCategory/getSearchPropertyByCategory/{id}")
    public Response getSearchPropertyByCategory(@PathVariable("id") String id) {
        return productCategoryService.getSearchPropertyByCategory(id);
    }

    /**
     * 根据一级分类id查三级分类
     * @param id
     * @return
     */
    @GetMapping("/productCategory/getThirdCategoryByTopId/{id}")
    public Response getThirdCategoryByTopId(@PathVariable("id") String id){
        return productCategoryService.getThirdCategoryByTopId(id);
    }
}
